﻿using System.Threading;
using System.Web;

namespace Aocsoft
{
    public class ContextStorage
    {
        public static void Save<T>(string key, T value)
        {
            if (HttpContext.Current != null)
            {
                if (HttpContext.Current.Items.Contains(key))
                {
                    HttpContext.Current.Items.Remove(key);
                }
                HttpContext.Current.Items.Add(key, value);
            }
            else
            {
                Thread.SetData(Thread.GetNamedDataSlot(key), value);
            }
        }

        public static T Load<T>(string key)
        {
            if (HttpContext.Current != null)
            {
                return (T)HttpContext.Current.Items[key];
            }
            else
            {
                return (T)Thread.GetData(Thread.GetNamedDataSlot(key));
            }
        }
    }
}
